'use strict';

import * as actions from '../../actions/home';

// 对页面prop 数据进行管理
const initialState = {
	pageSize:10,
	curPage:1,
	total:0,
	pages:[],
	msg_list:[]
};
export default function index(state = initialState, action = defaultAction ) {
    switch (action.type) {
		case actions.GET_PAGES:
			return Object.assign({}, state, {
				loginError:false,
				creating:false,
				error:false,
				pages:action.pages,
				total:action.total
			});
			break;
		case actions.PUBLISH_PAGE:
			{
				let page = state.pages.filter(c => c.id === action.page_id)[0];
					page.status="pubing";
				let sIndex = state.pages.indexOf(page);
				state.pages.splice(sIndex,1,page);
				return Object.assign({}, state, {
					loginError:false,
					creating:false,
					error:false,
				});
			}
			break;
		case actions.UN_PUBLISH_PAGE:
			{
				let page = state.pages.filter(c => c.id === action.page_id)[0];
					page.status="offline";
				let sIndex = state.pages.indexOf(page);
				state.pages.splice(sIndex,1,page);
				return Object.assign({}, state, {
					loginError:false,
					creating:false,
					error:false,
				});
			}
			break;
		case actions.EDIT_PAGE_TITLE:
			{
				let page = state.pages.filter(c => c.id === action.page_id)[0];
					page.name=action.name;
					page.preview=action.preview;
				let sIndex = state.pages.indexOf(page);
				state.pages.splice(sIndex,1,page);
				return Object.assign({}, state, {
					loginError:false,
					creating:false,
					error:false,
				});
			}
			break;
		case actions.PAGE_PUB_ERROR:
			return Object.assign({}, state, {
				loginError:false,
				creating:false,
				error:false,
				msg_list:action.msg_list
			});
			break;
		case actions.REMOVE_PAGE:
			let pages = state.pages;
			let page = pages.filter(c => c.id === action.page_id)[0];
			let sIndex = pages.indexOf(page);
			pages.splice(sIndex,1);
			return Object.assign({}, state, {
				loginError:false,
				creating:false,
				error:false,
				pages:pages,
				total:state.total-1
			});
			break;
		case actions.CLOSE_DIALOG:
			return Object.assign({}, state, {
				loginError:false,
				creating:false,
				error:false
			});
			break;
		case actions.LOAD_START:
		  return Object.assign({}, state, {
			loginError: false,
			message: "",
			error:false,
			creating:true,
		  });
		  break;
		case actions.LOAD_ERR:
		  return Object.assign({}, state, {
			loginError: action.loginFalse,
			message: action.message,
			error:true,
			creating:false
		  });
		  break;
		default:
		  return state;
	}
}
